/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessions;

import entities.Account;
import java.util.LinkedList;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author kheirarahali
 */
@Stateless
public class AccountFacade extends AbstractFacade<Account> implements AccountFacadeLocal {
    @PersistenceContext(unitName = "AdopteUnPrestaEJBPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AccountFacade() {
        super(Account.class);
    }
    
    @Override
    public Account authenticate(String login, String password) 
    {
        TypedQuery<Account> findByCredentials = 
                this.em.createNamedQuery("Account.findByCredentials", Account.class);
        findByCredentials.setParameter("login", login);
        findByCredentials.setParameter("password", password);
		
        LinkedList<Account> resultList = new LinkedList<Account>(findByCredentials.getResultList());

        if (!resultList.isEmpty()) 
        {
            return resultList.getFirst();
        } 
        else 
        {
            return null;
        }	
    }
    
}
